<?php
/**
 * 分页类
 *
 * @copyright (c) 2006 - 2007 与非网 (www.eefocus.com)
 * @author	kokko<kokko313@gmail.com>
 * @package com
 * @subpackage eefocus
 * @version 1.0 2007-01-07
 */
class Page{
	/**
     * 分页总页面数
	 *
     * @var integer
     * @access protected
     */
	protected $_totalPages;

	/**
     * 总行数
	 *
     * @var integer
     * @access protected
     */
	protected $_totalRows;

	/**
     * 当前页数
	 *
     * @var integer
     * @access protected
     */
    protected $_currentPage;

	/**
     * 每页显示行数
	 *
     * @var integer
     * @access protected
     */
    protected $_pageSize;


	/**
     * 分页变量
	 *
     * @var integer
     * @access protected
     */
    protected $_pageVar = 'page';
	
	/**
     * 分页URL
	 *
     * @var string
     * @access protected
     */
    protected $_pageUrl = '';

	/**
     * 分页栏每页显示的页数
	 *
     * @var integer
     * @access protected
     */
    protected $_rollPage = 5;

	/**
	 * 架构函数
	 *
	 * @access public 
	 * @param integer $totalRows  总的记录数
	 * @param integer $pageSize  每页显示记录数
	 */
	public function __construct($totalRows,$pageSize='')
	{
		$this->_totalRows = $totalRows;
		$this->_pageSize = !empty($pageSize)?$pageSize:20;
	}

	/**
	 * 设置分页url链接
	 *
	 * @access public
	 * @param integer $totalRows
	 */
	public function setTotalRows( $totalRows ){
		$this->_totalRows = $totalRows;
	}
	
	/**
	 * 设置分页栏数
	 *
	 * @access public
	 * @param integer $rollPage
	 */
	public function setRollPage( $rollPage ){
		$this->_rollPage = $rollPage;
	}
	
	/**
	 * 设置分页大小
	 *
	 * @access public
	 * @param integer $size
	 */
	public function setPageSize( $size ){
		$this->_pageSize = $size;
	}
	
	/**
	 * 获得分页数据
	 *
	 * @access public
	 * @return array
	 */
	public function getPage(){
		//当前页
		$this->_currentPage  = !empty( $_REQUEST[$this->_pageVar] ) ? intval( $_REQUEST[$this->_pageVar] ) : 1;

		//总页数
		$this->_totalPages = ceil($this->_totalRows/$this->_pageSize);

		//分析get url链接
		$this->getURL();

		$data = array();
		$data['pageUrl'] = $this->_pageUrl;
		$data['currentPage'] = $this->_currentPage;
		$data['pageSize'] = $this->_pageSize;
		$data['totalRows'] = $this->_totalRows;
		$data['totalPages'] = $this->_totalPages;
		$data['limitStart'] = $this->_pageSize*( $this->_currentPage -1 );
		$data['limitEnd'] = $this->_pageSize;
		$data['upPage'] = ( $this->_currentPage-1 > 0 ) ? $this->_currentPage-1 : 0;
		$data['nextPage'] = ( $this->_currentPage+1 < $this->_totalPages ) ? $this->_currentPage+1 : $this->_totalPages;
		$data['rollPage'] = $this->_rollPage;
		$data['linkPage'] = $this->getPageNav();
		return $data;
	}
	
	/**
	 * 获得数字分页起始
	 *
	 * @access protected
	 * @param array
	 */
	protected function getPageNav(){
		$start = ($this->_currentPage-$this->_rollPage);
		$end = $this->_currentPage+$this->_rollPage;
		
		if( $this->_totalPages<=1 ){
			return;
		}
		
		if( $this->_currentPage>$this->_rollPage ){
			$start = ($this->_currentPage-$this->_rollPage);
			$end = $this->_currentPage+$this->_rollPage;
		}else{
			$start = 1;
			$end = $this->_rollPage*2;
		}

		if( $end>=$this->_totalPages )
		{
			$end = $this->_totalPages;
		}
		for( $i=$start;$i<=$end;$i++ ){
			$page[] = $i;
		}
		return $page;
	}

	/**
	 * 分析get url链接
	 *
	 * @access protected 
	 */
	protected function getURL(){
		if( $this->_pageUrl == '' ){
			$url_query = http_build_query( $_GET );
			$url_query = ereg_replace("(^|&){$this->_pageVar}={$this->_currentPage}","",$url_query);
			$url = $_SERVER['SCRIPT_NAME']."?".$url_query;
			$this->_pageUrl = $url;
		}
	}
}
?>